﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类ChangeRate。
    /// </summary>
    public class ChangeRateDAL
    {
        public ChangeRateDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("tb_ChangeRate", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.ChangeRate model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_ChangeRate(");
            strSql.Append("Years,Powers,TelecomRate,BaseRate,BusinessRate,fTelecomRate,fBaseRate,fBusinessRate,Type,OperatUser,OperatDate)");
            strSql.Append(" values (");
            strSql.Append("@Years,@Powers,@TelecomRate,@BaseRate,@BusinessRate,@fTelecomRate,@fBaseRate,@fBusinessRate,@Type,@OperatUser,@OperatDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@Powers", SqlDbType.Decimal,13),
					new SqlParameter("@TelecomRate", SqlDbType.Decimal,5),
					new SqlParameter("@BaseRate", SqlDbType.Decimal,5),
					new SqlParameter("@BusinessRate", SqlDbType.Decimal,5),
					new SqlParameter("@fTelecomRate", SqlDbType.Decimal,5),
					new SqlParameter("@fBaseRate", SqlDbType.Decimal,5),
					new SqlParameter("@fBusinessRate", SqlDbType.Decimal,5),
					new SqlParameter("@Type", SqlDbType.Char,1),
					new SqlParameter("@OperatUser", SqlDbType.DateTime),
					new SqlParameter("@OperatDate", SqlDbType.DateTime)};
            parameters[0].Value = model.Years;
            parameters[1].Value = model.Powers;
            parameters[2].Value = model.TelecomRate;
            parameters[3].Value = model.BaseRate;
            parameters[4].Value = model.BusinessRate;
            parameters[5].Value = model.fTelecomRate;
            parameters[6].Value = model.fBaseRate;
            parameters[7].Value = model.fBusinessRate;
            parameters[8].Value = model.Type;
            parameters[9].Value = model.OperatUser;
            parameters[10].Value = model.OperatDate;


            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.ChangeRate model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_ChangeRate set ");
            strSql.Append("Years=@Years,");
            strSql.Append("Powers=@Powers,");
            strSql.Append("TelecomRate=@TelecomRate,");
            strSql.Append("BaseRate=@BaseRate,");
            strSql.Append("BusinessRate=@BusinessRate,");
            strSql.Append("fTelecomRate=@fTelecomRate,");
            strSql.Append("fBaseRate=@fBaseRate,");
            strSql.Append("fBusinessRate=@fBusinessRate,");
            strSql.Append("Type=@Type,");
            strSql.Append("OperatUser=@OperatUser,");
            strSql.Append("OperatDate=@OperatDate");
            strSql.Append(" where CR_ID=@CR_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CR_ID", SqlDbType.Int,4),
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@Powers", SqlDbType.Decimal,13),
					new SqlParameter("@TelecomRate", SqlDbType.Decimal,5),
					new SqlParameter("@BaseRate", SqlDbType.Decimal,5),
					new SqlParameter("@BusinessRate", SqlDbType.Decimal,5),
					new SqlParameter("@fTelecomRate", SqlDbType.Decimal,5),
					new SqlParameter("@fBaseRate", SqlDbType.Decimal,5),
					new SqlParameter("@fBusinessRate", SqlDbType.Decimal,5),
					new SqlParameter("@Type", SqlDbType.Char,1),
					new SqlParameter("@OperatUser", SqlDbType.DateTime),
					new SqlParameter("@OperatDate", SqlDbType.DateTime)};
            parameters[0].Value = model.CR_ID;
            parameters[1].Value = model.Years;
            parameters[2].Value = model.Powers;
            parameters[3].Value = model.TelecomRate;
            parameters[4].Value = model.BaseRate;
            parameters[5].Value = model.BusinessRate;
            parameters[6].Value = model.fTelecomRate;
            parameters[7].Value = model.fBaseRate;
            parameters[8].Value = model.fBusinessRate;
            parameters[9].Value = model.Type;
            parameters[10].Value = model.OperatUser;
            parameters[11].Value = model.OperatDate;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sIDs)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_ChangeRate ");
            strSql.Append(" where CR_ID in ( " + sIDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.ChangeRate GetModel(int CR_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CR_ID,Years,Powers,TelecomRate,BaseRate,BusinessRate,fTelecomRate,fBaseRate,fBusinessRate,Type,OperatUser,OperatDate from tb_ChangeRate ");
            strSql.Append(" where CR_ID=@CR_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CR_ID", SqlDbType.Int,4)};
            parameters[0].Value = CR_ID;

            HsTec.Mobile.Green.Model.ChangeRate model = new HsTec.Mobile.Green.Model.ChangeRate();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CR_ID"].ToString() != "")
                {
                    model.CR_ID = int.Parse(ds.Tables[0].Rows[0]["CR_ID"].ToString());
                }
                model.Years = ds.Tables[0].Rows[0]["Years"].ToString();
                if (ds.Tables[0].Rows[0]["Powers"].ToString() != "")
                {
                    model.Powers = decimal.Parse(ds.Tables[0].Rows[0]["Powers"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TelecomRate"].ToString() != "")
                {
                    model.TelecomRate = decimal.Parse(ds.Tables[0].Rows[0]["TelecomRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BaseRate"].ToString() != "")
                {
                    model.BaseRate = decimal.Parse(ds.Tables[0].Rows[0]["BaseRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BusinessRate"].ToString() != "")
                {
                    model.BusinessRate = decimal.Parse(ds.Tables[0].Rows[0]["BusinessRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fTelecomRate"].ToString() != "")
                {
                    model.fTelecomRate = decimal.Parse(ds.Tables[0].Rows[0]["fTelecomRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fBaseRate"].ToString() != "")
                {
                    model.fBaseRate = decimal.Parse(ds.Tables[0].Rows[0]["fBaseRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["fBusinessRate"].ToString() != "")
                {
                    model.fBusinessRate = decimal.Parse(ds.Tables[0].Rows[0]["fBusinessRate"].ToString());
                }
                model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                if (ds.Tables[0].Rows[0]["OperatUser"].ToString() != "")
                {
                    model.OperatUser = DateTime.Parse(ds.Tables[0].Rows[0]["OperatUser"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OperatDate"].ToString() != "")
                {
                    model.OperatDate = DateTime.Parse(ds.Tables[0].Rows[0]["OperatDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CR_ID,Years,TelecomRate,BaseRate,BusinessRate,fTelecomRate,fBaseRate,fBusinessRate,Type,OperatUser,OperatDate,Powers ");
            strSql.Append(" FROM tb_ChangeRate ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where (1=1) " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" CR_ID,Years,TelecomRate,BaseRate,BusinessRate,fTelecomRate,fBaseRate,fBusinessRate,Type,OperatUser,OperatDate,Powers ");
            strSql.Append(" FROM tb_ChangeRate ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

